class DisruptorProjectile extends TFProjectile
	placeable;

#exec OBJ LOAD FILE=XEffectMat.utx
#exec obj load file=..\Sounds\WeaponSounds.uax

var xEmitter Trail,Trail2;


simulated function Destroyed()
{
    if (Trail != None)
        Trail.Destroy();
    if (Trail2 != None)
        Trail2.Destroy();

	Super.Destroyed();
}

simulated function Timer()
{
	HurtRadius(Damage*0.15, 100, MyDamageType, 0, self.Location );
}

simulated function PostBeginPlay()
{
    local Rotator R;

	Super.PostBeginPlay();

    SetTimer(0.1, true);

//  if ( EffectIsRelevant(vect(0,0,0),false) )
		Trail = Spawn(class'DisruptorTrail',self);
    if ( EffectIsRelevant(vect(0,0,0),false) && !Level.bDropDetail && Level.DetailMode != DM_Low)
		Trail2 = Spawn(class'DisruptorSmokeTrail',self);

	Spawn(class'DisruptorFlare',self);


	Velocity = Vector(Rotation);
    Acceleration = Velocity * 3000.0;
    Velocity *= Speed;

    R = Rotation;
//    R.Pitch += 16000;
    R.Roll = Rand(65536);
    SetRotation(R);

    if ( Level.bDropDetail )
	{
		bDynamicLight = false;
		LightType = LT_None;
	}
}


simulated function PostNetBeginPlay()
{
	if ( Level.bDropDetail || Level.DetailMode == DM_Low )
		LightType = LT_None;
}

simulated function Explode(vector HitLocation, vector HitNormal)
{
//	Local xEmitter LFlame;

    if ( EffectIsRelevant(Location,false) )
	{
        if ( !Level.bDropDetail )
	        Spawn(class'ExplosionCrap',,, HitLocation, rotator(HitNormal));

        Spawn(class'LinkProjSparks',,, HitLocation, rotator(HitNormal));;
	Spawn(class'DisruptorExplosionA',,,HitLocation + HitNormal*16,rotator(HitNormal));
	Spawn(class'DisruptorExplosionB',,,HitLocation + HitNormal*16,rotator(HitNormal));

	HurtRadius(Damage*0.4, 200, MyDamageType, MomentumTransfer, Location );
	DelayedHurtRadius(1, 360, MyDamageType, 1, Location );

	if (Level.NetMode != NM_DedicatedServer)
	Spawn(class'DisruptorImpactGlow',self,,Location, rotator(-HitNormal));


	}
//    PlaySound(Sound'WeaponSounds.PulseRifle.PulseRifleAltFire');
//    PlaySound(Sound'WeaponSounds.BioRifle.BioRifleGoo2');
      PlaySound(Sound'TFMedia.DisruptorExplode',, 4*TransientSoundVolume);
	Destroy();
}

simulated function DelayedHurtRadius( float DamageAmount, float DamageRadius, class<DamageType> DamageType, float Momentum, vector HitLocation )
{
	local Pawn Victims;
	local float damageScale, dist;
	local vector dir;
        local DisruptorBurnDamage DBD;

	if( bHurtEntry )
		return;

	bHurtEntry = true;
	foreach VisibleCollidingActors( class 'Pawn', Victims, DamageRadius, HitLocation )
	{
		// don't let blast damage affect fluid - VisibleCollisingActors doesn't really work for them - jag
		if( (Victims != self) && (Victims.Role == ROLE_Authority) && (!Victims.IsA('FluidSurfaceInfo')) )
		{
			dir = Victims.Location - HitLocation;
			dist = FMax(1,VSize(dir));
			dir = dir/dist;
			damageScale = 1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius);

			Victims.TakeDamage
			(
				damageScale * DamageAmount,
				Instigator,
				Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dir,
				(damageScale * Momentum * dir),
				DamageType
			);

			foreach DynamicActors( class'DisruptorBurnDamage', DBD )
			{
				if ( DBD.Base == Victims )
					DBD.Destroy();
			}
			DBD = Spawn(Class'DisruptorBurnDamage',Instigator,,HitLocation);
			DBD.SetBase(Victims);
		}
	}
	bHurtEntry = false;
}

simulated function ProcessTouch (Actor Other, vector HitLocation)
{
    local Vector X, RefNormal, RefDir;
//    local DisruptorBurnDamage DBD;

	if (Other == Instigator) return;
    if (Other == Owner) return;

    if (Other.IsA('xPawn') && xPawn(Other).CheckReflect(HitLocation, RefNormal, Damage*0.25))
    {
        if (Role == ROLE_Authority)
        {
            X = Normal(Velocity);
            RefDir = X - 2.0*RefNormal*(X dot RefNormal);
            //Log("reflecting off"@Other@X@RefDir);
            Spawn(Class, Other,, HitLocation+RefDir*20, Rotator(RefDir));
        }
//        Destroy();
    }
    else if ( !Other.IsA('Projectile') || Other.bProjTarget)
	{
		if ( Role == ROLE_Authority )
			Other.TakeDamage(Damage ,Instigator,HitLocation,MomentumTransfer*Normal(Velocity),MyDamageType);

	if ( Pawn(Other) != None )
	{
		if ( xPawn(Other) != None && xPawn(Other).SkeletonMesh != None )
		{
				SetOnFire( xPawn(Other) );
				//SetOnFire( xPawn(Other) );
				//SetOnFire( xPawn(Other) );
		}
	}
		Explode(HitLocation, vect(0,0,1));
	}
}


simulated function SetOnFire( xPawn P )
{
	local float z;
	local name bonename;
	local DisruptorBodyFlameBig flame;

	z = Frand();
	flame = P.Spawn( class'DisruptorBodyFlameBig', P,, P.Location, P.Rotation );


		if( z < 0.10)
			boneName = 'Bip01 L Calf';
		else if (z < 0.20)
			boneName = 'Bip01 R Calf';
		else if (z < 0.30)
			boneName = 'Bip01 R Forearm';
		else if (z < 0.40)
			boneName = 'Bip01 L Forearm';
		else if (z < 0.50)
			boneName = 'bip01 spine';
		else if (z < 0.60)
			boneName = 'Bip01 L UpperArm';
		else if (z < 0.70)
			boneName = 'Bip01 R UpperArm';
		else if (z < 0.80)
			boneName = 'bip01 spine2';
		else
			boneName = 'Bip01 Head';

	if (!P.AttachToBone(flame,bonename))
		{
			if (!P.AttachToBone(flame,'Bip01 Head'))
			{
				//flame.Destroy();
				flame.Kill();
			}
		}

}

defaultproperties
{
     Speed=2000.000000
     MaxSpeed=4000.000000
     Damage=30.000000
     MomentumTransfer=45000.000000
     MyDamageType=Class'Taskforces.DisruptorDamType'
     ExplosionDecal=Class'XEffects.RocketMark'
     LightType=LT_Steady
     LightHue=80
     LightSaturation=120
     LightBrightness=160.000000
     LightRadius=5.000000
     DrawType=DT_StaticMesh
     StaticMesh=StaticMesh'AWStellarMeshes.Skies.Skyball'
     bDynamicLight=True
     AmbientSound=Sound'WeaponSounds.LinkGun.LinkGunProjectile'
     LifeSpan=10.000000
     DrawScale=0.125000
     PrePivot=(X=10.000000)
     Skins(0)=FinalBlend'TFMedia.Disruptor.DisruptorBlastTex'
     AmbientGlow=217
     Style=STY_Additive
     SoundVolume=255
     SoundRadius=50.000000
     bFixedRotationDir=True
     RotationRate=(Roll=40000)
     ForceType=FT_Constant
     ForceRadius=30.000000
     ForceScale=5.000000
}
